package com.xcw.bos.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.xcw.bos.domain.Workordermanage;
import com.xcw.bos.service.IWorkordermanageService;
import com.xcw.bos.utils.BosContext;
/**
 * 任务操作Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport {
    @Autowired
    private ProcessEngine processEngine;
    @Resource
    private IWorkordermanageService workordermanageService;
    
    // 查询组任务
    public String findGroupTask(){
    	// 任务查询对象
    	TaskQuery query = processEngine.getTaskService().createTaskQuery();
    	String candidateUser = BosContext.getLoginUser().getId();
    	
    	// 查询组任务
    	query.taskCandidateUser(candidateUser);
    	List<Task> list = query.list();
    	ActionContext.getContext().getValueStack().set("list", list);
    	return "groupTaskList";
    }
    
    private String taskId;// 任务id
    
    public String getTaskId(){
    	return taskId;
    }
    
    public void setTaskId(String taskId){
    	this.taskId = taskId;
    }
    
    public String findData() throws IOException{
    	Map<String, Object> map = processEngine.getTaskService().getVariables(taskId);
    	ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
    	ServletActionContext.getResponse().getWriter().print(map);
    	return NONE;
    }
    
    // 拾取组任务
    public String taskTask(){
    	processEngine.getTaskService().claim(taskId, BosContext.getLoginUser().getId());
    	return "toGroupTaskList";
    }
    
     // 查询个人任务
    public String findPersonalTask(){
    	TaskQuery query = processEngine.getTaskService().createTaskQuery();
    	String assignee = BosContext.getLoginUser().getId();// 登录用户id
    	query.taskAssignee(assignee);// 根据用户id过滤
    	query.orderByTaskCreateTime().desc();// 添加排序，根据任务创建时间降序
    	List<Task> list = query.list();
    	ActionContext.getContext().getValueStack().set("list", list);
    	return "personalTaskList";
    }
    
    private Integer check;// 审核结果 1:通过 0：不通过
    
    public Integer getCheck(){
    	return check;
    }
    
    public void setCheck(Integer check){
    	this.check = check;
    }
    
    // 办理审核工作单任务
    public String checkWorkOrderManage(){
    	if(check == null){
    		// 跳转到审核工作单页面
    		// 获取工作单数据
    		Task task = processEngine.getTaskService().createTaskQuery().taskId(taskId).singleResult();
    		String processInstanceId = task.getProcessInstanceId();
    		ProcessInstance processInstance = processEngine.getRuntimeService()
    				.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
    		String workId = processInstance.getBusinessKey();
    		Workordermanage workordermanage = workordermanageService.findById(workId);
    		ActionContext.getContext().getValueStack().push(workordermanage);
    		return "checkForm";
    	}else{
    		// 办理审核工作单任务,设置流程变量----check
    		Map<String, Object> variables = new HashMap<String, Object>();
    		variables.put("check", check);
    		variables.put("审核人员", BosContext.getLoginUser().getUsername());
    		processEngine.getTaskService().complete(taskId, variables);
    		return "toPersonalTaskList";
    	}
    }
    //办理货物出库任务
    public String outStore(){
    	Map<String, Object> variables =  new HashMap<String, Object>();
    	variables.put("出库操作员", BosContext.getLoginUser().getUsername());
    	processEngine.getTaskService().complete(taskId, variables);
    	return "toPersonalTaskList";
    }
    //办理配送签收任务
    public String receive(){
    	Map<String, Object> variables = new HashMap<String, Object>();
    	variables.put("取派员", BosContext.getLoginUser().getUsername());
    	processEngine.getTaskService().complete(taskId, variables);
    	return "toPersonalTaskList";
    }
    //办理货物配送任务
    public String transferGoods(){
    	Map<String, Object> variables = new HashMap<String, Object>();
    	variables.put("取派员", BosContext.getLoginUser().getUsername());
    	processEngine.getTaskService().complete(taskId, variables);
    	return "toPersonalTaskList";
    }
}
